var fs =        require("fs");
var path =      require("path");

var root = path.join(__dirname, '../../../app'), list = ['@import "../../app.scss";'];

var readDirSync = (_path, list = []) => {
    var pa = fs.readdirSync(_path);
    pa.forEach(m => {
        var p = `${_path}/${m}`, info = fs.statSync(p);
        if(info.isDirectory()){
            return readDirSync(p, list);
        }
        if(p.endsWith('.scss')){
            var filePath = path.resolve(_path, m), dir = path.relative(__dirname, filePath);
            var str = fs.readFileSync(filePath, 'utf8');
            if(/url\("(.+)"\)/.test(str)){
                list.push(str.replace(/url\("(\.\.\/)+assets\/imgs/, `url("../../assets/imgs`));
                return ;
            }
            list.push(`@import "${dir.replace(/\\/g, '/')}";`);
        }
    })
};

readDirSync(root, list);

fs.writeFile(path.resolve(__dirname, './common.scss'), list.join('\r\n'), 'utf8', err => {
    if(!err){
        console.log('import done!');
    }
});
